﻿using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SPCoder.Utils.Nodes
{
    public class WebNode : BaseNode
    {
         public WebNode()
        {
            //default icon for web
            base.Icon = "siteIcon.png";            
        }

         public WebNode(Web web) : this()
        {
            base.Title = web.Title;
            base.SPObjectType = web.GetType().Name;
            base.Url = web.ServerRelativeUrl;            
        }

        public override object GetRealSPObject()
        {
            if (RootNode != null && RootNode.SPObject != null)
            {                               
                if (RootNode.SPObject is Site)
                {
                    Site site = RootNode.SPObject as Site;
                    Web web = site.OpenWeb(base.Url);
                    web.Context.Load(web);
                    web.Context.ExecuteQuery();
                    return web;
                }                
            }
            return null;            
        }
    }
}